package com.yilin.platform.controller.capital;


import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.base_core.commons.utils.StringUtil;
import com.yilin.platform.entity.capital.AdvanceMoney;
import com.yilin.platform.entity.truck.TruckVehicle;
import com.yilin.platform.entity.user.auth.IdCard;
import com.yilin.platform.service.impl.capital.AdvanceMoneyService;
import com.yilin.platform.service.impl.truck.TruckVehicleService;
import com.yilin.platform.service.impl.user.AuthService;
import com.yilin.platform.service.impl.user.AuthorityService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/capital/advance/")
public class AdvanceMoneyController extends BaseController {
    @Resource
    private AdvanceMoneyService advanceMoneyService;
    @Resource
    private TruckVehicleService truckVehicleService;
    @Resource
    private AuthorityService authorityService;
    @Resource
    private AuthService authService;
    @PostMapping("saveOrUpdateAdvanceMoneySubtract")
    public ReturnData<Object> saveOrUpdateAdvanceMoneySubtract(AdvanceMoney advanceMoney) {
        IdCard idCard = authService.getIdCardByUserId(getLoginUser().getId());
        ParamUtil.isNull(idCard, "您还没有认证身份证，请先认证身份证");
        ParamUtil.isNull(advanceMoney, "请传入参数");
        TruckVehicle truckVehicle = null;
        ParamUtil.isNull(advanceMoney.getExpenseType(), "报销类型不能为空");
        ParamUtil.isNull(advanceMoney.getMonery(), "请输入报销金额");
        ParamUtil.isNull(advanceMoney.getOtherCostImgUrl(), "请传入报销证明");
        if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.oill || advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.vehicle) {
            ParamUtil.isNull(advanceMoney.getTruckId(), "请传入车辆");
            truckVehicle =truckVehicleService.getVehicleById(advanceMoney.getTruckId());
            ParamUtil.isNull(truckVehicle, "车辆获取失败");
            advanceMoney.setTruckNo(truckVehicle.getTrackNo());
        }
        advanceMoneyService.saveOrUpdateAdvanceMoneySubtract(getLoginUser(), advanceMoney, truckVehicle);
        return jsonView(true, "保存成功");
    }

    @PostMapping("saveOrUpdateAdvanceMoneyAdd")
    public ReturnData<Object> saveOrUpdateAdvanceMoneyAdd(AdvanceMoney advanceMoney, String accountId,Boolean negative) {
        ParamUtil.isNull(advanceMoney, "请传入参数");
        ParamUtil.isNull(advanceMoney.getExpenseType(), "报销类型不能为空");
        ParamUtil.isNull(advanceMoney.getMonery(), "请输入报销金额");
//        ParamUtil.isNull(advanceMoney.getOtherCostImgUrl(), "请传入报销证明");
        TruckVehicle truckVehicle = null;
        if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.oill || advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.vehicle|| advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.integral) {
            ParamUtil.isNull(advanceMoney.getTruckId(), "请传入车辆");
            truckVehicle = truckVehicleService.getVehicleById(advanceMoney.getTruckId());
            ParamUtil.isNull(truckVehicle, "车辆获取失败");
        }
        if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.monery){
            authorityService.getAuthorityByAccountIdAndCode(getLoginUser(),"1006");
        }
        if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.integral){
            authorityService.getAuthorityByAccountIdAndCode(getLoginUser(),"1007");
        }
        if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.vehicle){
            authorityService.getAuthorityByAccountIdAndCode(getLoginUser(),"1009");
        }
        if(advanceMoney.getExpenseType()== AdvanceMoney.ExpenseType.oill){
            authorityService.getAuthorityByAccountIdAndCode(getLoginUser(),"1008");
        }
        advanceMoneyService.saveOrUpdateAdvanceMoneyAdd(getLoginUser(),accountId, advanceMoney, truckVehicle,negative);
        return jsonView(true, "保存成功");
    }

    @PostMapping("auditingAdvanceMoney")
    public ReturnData<Object> auditingAdvanceMoney(AdvanceMoney advanceMoney) {
        ParamUtil.isNull(advanceMoney, "请传入参数");
        ParamUtil.isNull(advanceMoney.getId(), "请传入id");
        ParamUtil.isNull(advanceMoney.getAdvanceState(), "请传入状态");
        if (advanceMoney.getAdvanceState() != AdvanceMoney.AdvanceState.create && advanceMoney.getAdvanceState() != AdvanceMoney.AdvanceState.success) {
            return jsonView(false, "状态错误");
        }
        if (advanceMoney.getAdvanceState() == AdvanceMoney.AdvanceState.create && StringUtil.isEmpty(advanceMoney.getResMsg())) {
            return jsonView(false, "请说明原因");
        }
        AdvanceMoney advanceMoney1=advanceMoneyService.getAdvanceMoneyById(advanceMoney.getId());
        if(advanceMoney1.getExpenseType()== AdvanceMoney.ExpenseType.travel||advanceMoney1.getExpenseType()== AdvanceMoney.ExpenseType.office){
            authorityService.getAuthorityByAccountIdAndCode(getLoginUser(),"1005");
        }
        if(advanceMoney1.getExpenseType()== AdvanceMoney.ExpenseType.oill){
            authorityService.getAuthorityByAccountIdAndCode(getLoginUser(),"1004");
        }
        if(advanceMoney1.getExpenseType()== AdvanceMoney.ExpenseType.vehicle){
            authorityService.getAuthorityByAccountIdAndCode(getLoginUser(),"1003");
        }
        advanceMoney1.setAdvanceState(advanceMoney.getAdvanceState());
        advanceMoney1.setResMsg(advanceMoney.getResMsg());
        advanceMoneyService.auditingAdvanceMoney(getLoginUser(), advanceMoney1);
        return jsonView(true, "保存成功");
    }
    @PostMapping("getEmpAccountStatic")
    public ReturnData<Object> getEmpAccountStatic(String accountId, String  startDate,String  endDate) {
        ParamUtil.isNull(accountId,"获取账户为空");
      Map<String,Object> data = advanceMoneyService.getEmpAccountStatic(accountId, startDate, endDate);
        return jsonView(SUCCESS_CODE, "获取成功", data);
    }


    @PostMapping("getAdvanceMoneyPage")
    public ReturnData<Object> getAdvanceMoneyPage(AdvanceMoney query, Integer page, Integer limit) {
        PageData<AdvanceMoney> data = advanceMoneyService.getAdvanceMoneyPage(getLoginUser(), query, page, limit);
        return jsonView(SUCCESS_CODE, "获取成功", data);
    }
    @PostMapping("getAdvanceMoneyByAccountPage")
    public ReturnData<Object> getAdvanceMoneyByAccountPage(AdvanceMoney query, Integer page, Integer limit) {
        ParamUtil.isNull(query.getAccountId(),"查询用户不能为空");
        PageData<AdvanceMoney> data = advanceMoneyService.getAdvanceMoneyByAccountPage( query, page, limit);
        return jsonView(SUCCESS_CODE, "获取成功", data);
    }



    @PostMapping("getAdvanceMoneyById")
    public ReturnData<Object> getAdvanceMoneyById(String advanceId) {
        AdvanceMoney data = advanceMoneyService.getAdvanceMoneyById(advanceId);
        return jsonView(SUCCESS_CODE, "获取成功", data);
    }
    @PostMapping("getOrganizeAdvaceStatic")
    public ReturnData<Object> getOrganizeAdvaceStatic() {
        Map<String,Object> data = advanceMoneyService.getOrganizeAdvaceStatic(getLoginUser().getOrganizeId());
        return jsonView(SUCCESS_CODE, "获取成功", data);
    }


}
